package com.zlcx.tz.live.vo.response;


import com.zlcx.tz.live.constant.ResultStatus;
import com.zlcx.tz.live.exception.CheckRunException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


@ApiModel(value = "返回值信息集合")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BusinessResult<T> implements Serializable {
    @ApiModelProperty(position = 10, value = "状态码")
    private Integer code;

    @ApiModelProperty(position = 20, value = "返回信息")
    private String message;

    @ApiModelProperty(position = 150, value = "返回结果")
    private T data;

    public static <T> BusinessResult<T> createSuccessInstance(T data) {
        return new BusinessResult<>(ResultStatus.SUCCESS.getCode(), ResultStatus.SUCCESS.getInfo(), data);
    }

    public static <T> BusinessResult<T> createInstance(ResultStatus message) {
        return new BusinessResult<>(message.getCode(), message.getInfo(), null);
    }

    public static <T> BusinessResult<T> createInstance(ResultStatus message,T data) {
        return new BusinessResult<>(message.getCode(), message.getInfo(), data);
    }

    public static <T> BusinessResult<T> createInstance(Integer code,String message,T data) {
        return new BusinessResult<>(code, message, data);
    }

    public static <T> BusinessResult<T> createInstance(Integer code,String message) {
        return new BusinessResult<>(code, message, null);
    }

    public static void checkBusinessResult(BusinessResult businessResult) {
        if (null == businessResult) {
            throw new CheckRunException("返回BusinessResult不能为空");
        }
        if (businessResult.getCode().longValue() != ResultStatus.SUCCESS.getCode()) {
            throw new CheckRunException("调用接口失败"+businessResult);
        }
    }


}
